package com.songdehuai.imageselect.picture;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;


public class PictureSelectorActivity extends AppCompatActivity {

    private boolean isCamera = false;

    private int maxCount = 9;

    public final static int CHOOSE_REQUEST = 9980;

    private List<LocalMedia> localMedia;

    private MyPictureSelector notificationCenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Translucent_NoTitleBar);
        initConfig();
    }


    void initConfig() {
        maxCount = getIntent().getIntExtra("MAX_COUNT", 0);
        isCamera = getIntent().getBooleanExtra("ISCAMERA", false);
        notificationCenter = MyPictureSelector.getInstance();
        if (maxCount == 1) {
            maxCount = PictureConfig.SINGLE;
        }
        // 进入相册 以下是例子：不需要的api可以不写
        com.luck.picture.lib.PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                .maxSelectNum(maxCount)// 最大图片选择数量 int
                .imageSpanCount(4)// 每行显示个数 int
                .selectionMode(maxCount)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .previewImage(true)// 是否可预览图片 true or false
                .previewVideo(true)// 是否可预览视频 true or false
                .enablePreviewAudio(true) // 是否可播放音频  true or false
                //.isCamera()// 是否显示拍照按钮 ture or false
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                .isGif(true)// 是否显示gif图片 true or false
                .previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                .forResult(CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CHOOSE_REQUEST) {
            localMedia = com.luck.picture.lib.PictureSelector.obtainMultipleResult(data);
            notificationCenter.postNotification(localMedia);
        }
        finish();
    }

    @Override
    protected void onPause() {
        overridePendingTransition(0, 0);
        super.onPause();
    }
}
